//
//  APPLEViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/15.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "APPLEViewController.h"

@interface APPLEViewController ()

@property(nonatomic,strong)UIViewController *mainviewcontroller;
@property(nonatomic,strong)UIViewController *leftviewcontroller;
@property(nonatomic,strong)UIViewController *rightViewController;
@property(nonatomic,strong)UIScreenEdgePanGestureRecognizer *gestureForOpenningLeftDawer;

@property(nonatomic,strong)UIScreenEdgePanGestureRecognizer *gestureForCloseingLeftDawer;



@property(nonnull,strong)UIView *blackView;

-(void)handleDrawerforLeft:(BOOL)isleft open:(BOOL)isopen;

-(void)initializeAppearance;

-(CGFloat)interpolateFrom:(CGFloat)from to:(CGFloat)to persent:(CGFloat)persent;
@end

@implementation APPLEViewController

#pragma mark -- initilize
-(instancetype)initWithMainViewController:(UIViewController *)mainviewcontroller leftViewController:(UIViewController *)leftviewcontroller rightViewController:(UIViewController *)rightViewController
{
    self = [super init];
    if (self) {
        
        self.mainviewcontroller = mainviewcontroller;
        self.leftviewcontroller = leftviewcontroller;
        self.rightViewController = rightViewController;
        
        
    }
    
    return self;
}

-(instancetype)initWithMainViewController:(UIViewController *)mainviewcontroller leftViewController:(UIViewController *)leftviewcontroller{
    
    return [self initWithMainViewController:mainviewcontroller leftViewController:leftviewcontroller rightViewController:nil];
}

#pragma mark -- life cycyle

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initializeAppearance];
    
}

-(UIView *)blackView{
    
    if (!_blackView) {
        _blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 75, 375, 592)];
        _blackView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    }
    
    return _blackView;
}
#pragma mark -- privatemethods
-(void)initializeAppearance{
    
    [self.mainviewcontroller.view addGestureRecognizer:self.gestureForOpenningLeftDawer];
    [self.blackView addGestureRecognizer:self.gestureForCloseingLeftDawer];
    
    [self addChildViewController:self.leftviewcontroller];
    [self.leftviewcontroller didMoveToParentViewController:self];
    
//    self.leftviewcontroller.view.transform = CGAffineTransformMakeScale(0.8, 0.8);
    
 
    
    
    
    if (self.rightViewController) {
        [self addChildViewController:self.rightViewController];
        [self.rightViewController didMoveToParentViewController:self];
//        self.rightViewController.view.transform = CGAffineTransformMakeScale(0.8, 0.8);
    }
    [self addChildViewController:self.mainviewcontroller];
    
    [self.view addSubview:self.mainviewcontroller.view];
    
    [self.mainviewcontroller didMoveToParentViewController:self];

    
}

#pragma mark -- privateMethods
-(void)handleDrawerforLeft:(BOOL)isleft open:(BOOL)isopen
{
    CGRect frame;
    CGFloat scale;
    void (^completionBlock)(BOOL flag) = nil;
    
//    int (^block)(int int);
//    
//    block = ^int(int a,int b){
//        
//    }
//    block = (2 ,2);
    if (isleft && isopen) {
        //打开左抽屉
        
        [self.view insertSubview:self.leftviewcontroller.view belowSubview:self.mainviewcontroller.view];
        [self.mainviewcontroller.view addSubview:self.blackView];
        self.blackView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6];
        
        self.leftOpen = YES;
        
        
        frame = CGRectMake(300, 0, 375, 667);
        scale = 1;
    }else if (isleft && !isopen){
                //关闭左抽屉
        [self.blackView removeFromSuperview];
        frame = self.view.bounds;
        scale = 1;
        
        completionBlock = ^(BOOL flag){
            
            [self.leftviewcontroller.view removeFromSuperview];
            
            self.blackView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
            self.leftOpen = NO;
        };
    }else if (!isleft && isopen){
        //有抽屉 打开
        
//        frame = CGRectMake(-300, 0, 375, 667);
//        [self.mainviewcontroller.view addSubview:self.blackView];
       
        [self.view insertSubview:self.rightViewController.view belowSubview:self.mainviewcontroller.view];
        [self.mainviewcontroller.view addSubview:_blackView];
        
        
         frame = CGRectMake( - 300 , 0, 375, 667);
        
        self.blackView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6];
        
        self.rightOpen =YES;

         scale = 1;
        
    }else{
        
        [self.blackView removeFromSuperview];
        frame = self.view.bounds;
        scale = 1;
        completionBlock = ^(BOOL flag){
             [self.rightViewController.view removeFromSuperview];
            self.mainviewcontroller.view.frame = self.view.bounds;
            
           
            
            [self.blackView removeFromSuperview];
            
            self.blackView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
            
            self.rightOpen = NO;

        };
        
        
    }

    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        self.mainviewcontroller.view.frame = frame;
        
        self.leftviewcontroller.view.transform = CGAffineTransformMakeScale(scale, scale);
    } completion:completionBlock];
    
    
}




//线性插值
-(CGFloat)interpolateFrom:(CGFloat)from to:(CGFloat)to persent:(CGFloat)persent{
    
    
    
    return from + (to - from) * persent;
}
-(void)openLeftDrawer{
    
    
    [self handleDrawerforLeft:YES open:YES];
}
-(void)closeLeftDrawer{
    [self handleDrawerforLeft:YES open:NO];
}
-(void)openRightDrawer{
    
    [self handleDrawerforLeft:NO open:YES];
}
-(void)closeRightDrawer{
    
    [self handleDrawerforLeft:NO open:NO];
}
#pragma mark -- callback/action

-(void)respondsToGesture:(UIScreenEdgePanGestureRecognizer *)gesture{
   
    if (gesture.state == UIGestureRecognizerStateBegan) {
         [self.view insertSubview:self.leftviewcontroller.view belowSubview:self.mainviewcontroller.view];
        [self.mainviewcontroller.view addSubview:self.blackView];
       
        
        
       
        
    }else if (gesture.state == UIGestureRecognizerStateChanged){
        CGPoint tanslation = [gesture translationInView:gesture.view];
        if (tanslation.x > 0) {
            
            CGFloat persent = tanslation.x/250.f;
            
            if (persent > 1) {
                
                persent = 1;
            }
            CGFloat scale = [self interpolateFrom:1 to:1 persent:persent];
            
            CGFloat x = [self interpolateFrom:0 to:300 persent:persent];
            
            CGFloat alpha = [self interpolateFrom:0 to:0.6 persent:persent];
            
            self.leftviewcontroller.view.transform = CGAffineTransformMakeScale(scale, scale);
            
            self.mainviewcontroller.view.frame = CGRectMake(x, 0, 375, 667);
            
            self.blackView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:alpha];
            
        }
       
    
    
    
    
    }else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled){
        
        
        CGPoint velocity = [gesture velocityInView:gesture.view];
        
        if (velocity.x > 0) {
            [self openLeftDrawer];
       
        }else{
            [self closeLeftDrawer];
        }
        
        
        
        
    }
    
}

-(void)respondsToGesture2:(UIScreenEdgePanGestureRecognizer *)gesture{
    
     if (gesture.state == UIGestureRecognizerStateChanged){
         
        

         CGPoint translation = [gesture translationInView:gesture.view];
         
         if (translation.x < 0) {
             CGFloat  persent = - translation.x/60.f;
             
             if (persent >  1) {
                 persent = 1;
             }
             
             CGFloat scale = [self interpolateFrom:1 to:1 persent:persent];
             
             CGFloat x = [self interpolateFrom:300 to:0 persent:persent];
             
             self.leftviewcontroller.view.transform = CGAffineTransformMakeScale(scale, scale);
             
             self.mainviewcontroller.view.frame = CGRectMake(x, 0, 375, 667);
             
             self.blackView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1 - persent];

            

             
             
             
             
            
         }
        
    }else if (gesture.state == UIGestureRecognizerStateEnded ||gesture.state == UIGestureRecognizerStateCancelled){
        
        CGPoint velocity = [gesture velocityInView:gesture.view];
        
        if (velocity.x < 0) {
            [self closeLeftDrawer];
        }else{
            [self openLeftDrawer];
        }
        
    }
    
}

#pragma mark -- getter
-(UIScreenEdgePanGestureRecognizer *)gestureForOpenningLeftDawer{
    
    if (!_gestureForOpenningLeftDawer) {
        _gestureForOpenningLeftDawer = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToGesture:)];
        
        _gestureForOpenningLeftDawer.edges = UIRectEdgeLeft;
    }
    return  _gestureForOpenningLeftDawer;
    
    
    }

-(UIScreenEdgePanGestureRecognizer *)gestureForCloseingLeftDawer{
    
    if (!_gestureForCloseingLeftDawer) {
        _gestureForCloseingLeftDawer = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(respondsToGesture2:)];
        
        _gestureForCloseingLeftDawer.edges = UIRectEdgeRight;
    }
    
    
    return _gestureForCloseingLeftDawer;
    
    }
@end
